package com.vimaltuts.mycontacts;

import com.vimaltuts.mycontacts.R;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomContactsAdapter extends SimpleCursorAdapter {

	private int layout;
	private ImageButton editBtn;
	private ImageButton delBtn;
	LayoutInflater inflator;

	public CustomContactsAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
		super(context, layout, c, from, to,0);
		this.layout = layout;
		inflator= LayoutInflater.from(context);
	}

	public View newView(Context context, Cursor cursor, ViewGroup parent) {		
		View v = inflator.inflate(layout, parent, false);
		return v;
	}

	@Override
	public void bindView(View v, final Context context, Cursor c) {
		final int id = c.getInt(c.getColumnIndex(Contacts.ID));
		final String name = c.getString(c.getColumnIndex(Contacts.NAME));
		final String phone = c.getString(c.getColumnIndex(Contacts.PHONE));
		final String email = c.getString(c.getColumnIndex(Contacts.MAIL));
		final String fb = c.getString(c.getColumnIndex(Contacts.FB));
		final byte[] image = c.getBlob(c.getColumnIndex(Contacts.IMAGE));
		ImageView iv = (ImageView) v.findViewById(R.id.photo);

		if (image != null) {
			if (image.length > 3) {
				iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length));
			}
		}

		TextView tname = (TextView) v.findViewById(R.id.name);
		tname.setText(name);
		TextView tphone = (TextView) v.findViewById(R.id.phone);
		tphone.setText(phone);
		TextView temail = (TextView) v.findViewById(R.id.email);
		temail.setText(email);
		
		final SQLiteConnector sqlCon = new SQLiteConnector(context);
		
		editBtn=(ImageButton) v.findViewById(R.id.edit_btn);	
		editBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context,AddEditContacts.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);				
				intent.putExtra("id", id);
				intent.putExtra("name", name);
				intent.putExtra("phone", phone);
				intent.putExtra("email", email);
				intent.putExtra("fb", fb);
				intent.putExtra("blob", image);
				context.startActivity(intent);
			}
			
		});
		
		delBtn=(ImageButton) v.findViewById(R.id.del_btn);	
		delBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sqlCon.deleteContact(id);	
				Intent intent=new Intent(context,MyContactsActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
			
		});		
		
		v.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context,ViewContact.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);				
				intent.putExtra("id", id);
				intent.putExtra("name", name);
				intent.putExtra("phone", phone);
				intent.putExtra("email", email);
				intent.putExtra("fb", fb);
				intent.putExtra("blob", image);
				context.startActivity(intent);
			}
		});
		
	}

}
